home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / svgamesa.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-13  |  13.6 KB  |  537 lines

  1. /* $Id: svgamesa.c,v 1.2 1996/10/15 00:23:51 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.0
  6.  * Copyright (C) 1995-1996  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: svgamesa.c,v $
  26.  * Revision 1.2  1996/10/15 00:23:51  brianp
  27.  * cleaned up code to work with Mesa 2.0
  28.  *
  29.  * Revision 1.1  1996/09/13 01:38:16  brianp
  30.  * Initial revision
  31.  *
  32.  */
  33.  
  34.  
  35. /*
  36.  * Linux SVGA/Mesa interface.
  37.  *
  38.  * This interface is not finished!  Still have to implement pixel
  39.  * reading functions and double buffering.  Then, look into accelerated
  40.  * line and polygon rendering.  And, clean up a bunch of other stuff.
  41.  * Any volunteers?
  42.  */
  43.  
  44.  
  45. #ifdef SVGA
  46.  
  47.  
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <vga.h>
  51. #include "GL/svgamesa.h"
  52. #include "context.h"
  53. #include "matrix.h"
  54. #include "types.h"
  55.  
  56.  
  57.  
  58. struct svgamesa_context {
  59.    GLcontext *gl_ctx;        /* the core Mesa context */
  60.    GLvisual *gl_vis;        /* describes the color buffer */
  61.    GLframebuffer *gl_buffer;    /* the ancillary buffers */
  62.    GLuint index;        /* current color index */
  63.    GLint red, green, blue;    /* current rgb color */
  64.    GLint width, height;        /* size of color buffer */
  65.    GLint depth;            /* bits per pixel (8,16,24 or 32) */
  66. };
  67.  
  68.  
  69. static SVGAMesaContext SVGAMesa = NULL;    /* the current context */
  70.  
  71.  
  72.  
  73. /*
  74.  * Convert Mesa window Y coordinate to VGA screen Y coordinate:
  75.  */
  76. #define FLIP(Y)  (SVGAMesa->height-(Y)-1)
  77.  
  78.  
  79.  
  80. /**********************************************************************/
  81. /*****                 Miscellaneous functions                    *****/
  82. /**********************************************************************/
  83.  
  84.  
  85. static void get_buffer_size( GLcontext *ctx, GLuint *width, GLuint *height )
  86. {
  87.    *width = SVGAMesa->width = vga_getxdim();
  88.    *height = SVGAMesa->height = vga_getydim();
  89. }
  90.  
  91.  
  92. /* Set current color index */
  93. static void set_index( GLcontext *ctx, GLuint index )
  94. {
  95.    SVGAMesa->index = index;
  96.    vga_setcolor( index );
  97. }
  98.  
  99.  
  100. /* Set current drawing color */
  101. static void set_color( GLcontext *ctx,
  102.                        GLubyte red, GLubyte green,
  103.                        GLubyte blue, GLubyte alpha )
  104. {
  105.    SVGAMesa->red = red;
  106.    SVGAMesa->green = green;
  107.    SVGAMesa->blue = blue;
  108.    vga_setrgbcolor( red, green, blue );
  109. }
  110.  
  111.  
  112. static void clear_index( GLcontext *ctx, GLuint index )
  113. {
  114.    /* TODO: Implements glClearIndex() */
  115. }
  116.  
  117.  
  118. static void clear_color( GLcontext *ctx,
  119.                          GLubyte red, GLubyte green,
  120.                          GLubyte blue, GLubyte alpha )
  121. {
  122.    /* TODO: Implements glClearColor() */
  123. }
  124.  
  125.  
  126. static void clear( GLcontext *ctx,
  127.                    GLboolean all,
  128.                    GLint x, GLint y, GLint width, GLint height )
  129. {
  130.    vga_clear();
  131. }
  132.  
  133.  
  134. static GLboolean set_buffer( GLcontext *ctx,
  135.                              GLenum mode )
  136. {
  137.    /* TODO: implement double buffering and use this function to select */
  138.    /* between front and back buffers. */
  139.    return GL_TRUE;
  140. }
  141.  
  142.  
  143.  
  144.  
  145. /**********************************************************************/
  146. /*****            Write spans of pixels                           *****/
  147. /**********************************************************************/
  148.  
  149.  
  150. static void write_index_span( GLcontext *ctx,
  151.                               GLuint n, GLint x, GLint y,
  152.                               const GLuint index[],
  153.                               const GLubyte mask[] )
  154. {
  155.    int i;
  156.    y = FLIP(y);
  157.    for (i=0;i<n;i++,x++) {
  158.       if (mask[i]) {
  159.          vga_setcolor( index[i] );
  160.          vga_drawpixel( x, y );
  161.       }
  162.    }
  163. }
  164.  
  165.  
  166.  
  167. static void write_monoindex_span( GLcontext *ctx,
  168.                                   GLuint n, GLint x, GLint y,
  169.                                   const GLubyte mask[] )
  170. {
  171.    int i;
  172.    y = FLIP(y);
  173.    /* use current color index */
  174.    vga_setcolor( SVGAMesa->index );
  175.    for (i=0;i<n;i++,x++) {
  176.       if (mask[i]) {
  177.          vga_drawpixel( x, y );
  178.       }
  179.    }
  180. }
  181.  
  182.  
  183.  
  184. static void write_color_span( GLcontext *ctx,
  185.                               GLuint n, GLint x, GLint y,
  186.                               const GLubyte red[], const GLubyte green[],
  187.                               const GLubyte blue[], const GLubyte alpha[],
  188.                               const GLubyte mask[] )
  189. {
  190.    int i;
  191.    y=FLIP(y);
  192.    if (mask) {
  193.       /* draw some pixels */
  194.       for (i=0; i<n; i++, x++) {
  195.          if (mask[i]) {
  196.             vga_setrgbcolor( red[i], green[i], blue[i] );
  197.             vga_drawpixel( x, y );
  198.          }
  199.       }
  200.    }
  201.    else {
  202.       /* draw all pixels */
  203.       for (i=0; i<n; i++, x++) {
  204.          vga_setrgbcolor( red[i], green[i], blue[i] );
  205.          vga_drawpixel( x, y );
  206.       }
  207.    }
  208. }
  209.  
  210.  
  211.  
  212. static void write_monocolor_span( GLcontext *ctx,
  213.                                   GLuint n, GLint x, GLint y,
  214.                                   const GLubyte mask[])
  215. {
  216.    int i;
  217.    y=FLIP(y);
  218.    /* use current rgb color */
  219.    vga_setrgbcolor( SVGAMesa->red, SVGAMesa->green, SVGAMesa->blue );
  220.    for (i=0; i<n; i++, x++) {
  221.       if (mask[i]) {
  222.          vga_drawpixel( x, y );
  223.       }
  224.    }
  225. }
  226.  
  227.  
  228.  
  229. /**********************************************************************/
  230. /*****                 Read spans of pixels                       *****/
  231. /**********************************************************************/
  232.  
  233.  
  234. static void read_index_span( GLcontext *ctx,
  235.                              GLuint n, GLint x, GLint y, GLuint index[])
  236. {
  237.    int i;
  238.    y = FLIP(y);
  239.    for (i=0; i<n; i++,x++) {
  240.       index[i] = vga_getpixel( x, y );
  241.    }
  242. }
  243.  
  244.  
  245.  
  246. static void read_color_span( GLcontext *ctx,
  247.                              GLuint n, GLint x, GLint y,
  248.                              GLubyte red[], GLubyte green[],
  249.                              GLubyte blue[], GLubyte alpha[] )
  250. {
  251.    int i;
  252.    for (i=0; i<n; i++, x++) {
  253.       /* TODO */
  254.    }
  255. }
  256.  
  257.  
  258.  
  259. /**********************************************************************/
  260. /*****                  Write arrays of pixels                    *****/
  261. /**********************************************************************/
  262.  
  263.  
  264. static void write_index_pixels( GLcontext *ctx,
  265.                                 GLuint n, const GLint x[], const GLint y[],
  266.                                 const GLuint index[], const GLubyte mask[] )
  267. {
  268.    int i;
  269.    for (i=0; i<n; i++) {
  270.       if (mask[i]) {
  271.          vga_setcolor( index[i] );
  272.          vga_drawpixel( x[i], FLIP(y[i]) );
  273.       }
  274.    }
  275. }
  276.  
  277.  
  278.  
  279. static void write_monoindex_pixels( GLcontext *ctx,
  280.                                     GLuint n,
  281.                                     const GLint x[], const GLint y[],
  282.                                     const GLubyte mask[] )
  283. {
  284.    int i;
  285.    /* use current color index */
  286.    vga_setcolor( SVGAMesa->index );
  287.    for (i=0; i<n; i++) {
  288.       if (mask[i]) {
  289.          vga_drawpixel( x[i], FLIP(y[i]) );
  290.       }
  291.    }
  292. }
  293.  
  294.  
  295.  
  296. static void write_color_pixels( GLcontext *ctx,
  297.                                 GLuint n, const GLint x[], const GLint y[],
  298.                                 const GLubyte r[], const GLubyte g[],
  299.                                 const GLubyte b[], const GLubyte a[],
  300.                                 const GLubyte mask[] )
  301. {
  302.    int i;
  303.    for (i=0; i<n; i++) {
  304.       if (mask[i]) {
  305.          vga_setrgbcolor( r[i], g[i], b[i] );
  306.          vga_drawpixel( x[i], FLIP(y[i]) );
  307.       }
  308.    }
  309. }
  310.  
  311.  
  312.  
  313. static void write_monocolor_pixels( GLcontext *ctx,
  314.                                     GLuint n,
  315.                                     const GLint x[], const GLint y[],
  316.                                     const GLubyte mask[] )
  317. {
  318.    int i;
  319.    /* use current rgb color */
  320.    vga_setrgbcolor( SVGAMesa->red, SVGAMesa->green, SVGAMesa->blue );
  321.    for (i=0; i<n; i++) {
  322.       if (mask[i]) {
  323.          vga_drawpixel( x[i], FLIP(y[i]) );
  324.       }
  325.    }
  326. }
  327.  
  328.  
  329.  
  330.  
  331. /**********************************************************************/
  332. /*****                   Read arrays of pixels                    *****/
  333. /**********************************************************************/
  334.  
  335. /* Read an array of color index pixels. */
  336. static void read_index_pixels( GLcontext *ctx,
  337.                                GLuint n, const GLint x[], const GLint y[],
  338.                                GLuint index[], const GLubyte mask[] )
  339. {
  340.    int i;
  341.    for (i=0; i<n; i++,x++) {
  342.       index[i] = vga_getpixel( x[i], FLIP(y[i]) );
  343.    }
  344. }
  345.  
  346.  
  347.  
  348. static void read_color_pixels( GLcontext *ctx,
  349.                                GLuint n, const GLint x[], const GLint y[],
  350.                                GLubyte red[], GLubyte green[],
  351.                                GLubyte blue[], GLubyte alpha[],
  352.                                const GLubyte mask[] )
  353. {
  354.    /* TODO */
  355. }
  356.  
  357.  
  358.  
  359. static void svgamesa_setup_DD_pointers( GLcontext *ctx )
  360. {
  361.    /* Initialize all the pointers in the DD struct.  Do this whenever */
  362.    /* a new context is made current or we change buffers via set_buffer! */
  363.  
  364.    ctx->Driver.UpdateState = svgamesa_setup_DD_pointers;
  365.  
  366.    ctx->Driver.ClearIndex = clear_index;
  367.    ctx->Driver.ClearColor = clear_color;
  368.    ctx->Driver.Clear = clear;
  369.  
  370.    ctx->Driver.Index = set_index;
  371.    ctx->Driver.Color = set_color;
  372.  
  373.    ctx->Driver.SetBuffer = set_buffer;
  374.    ctx->Driver.GetBufferSize = get_buffer_size;
  375.  
  376.    ctx->Driver.PointsFunc = NULL;
  377.    ctx->Driver.LineFunc = NULL;
  378.    ctx->Driver.TriangleFunc = NULL;
  379.  
  380.    /* Pixel/span writing functions: */
  381.    /* TODO: use different funcs for 8, 16, 32-bit depths */
  382.    ctx->Driver.WriteColorSpan       = write_color_span;
  383.    ctx->Driver.WriteMonocolorSpan   = write_monocolor_span;
  384.    ctx->Driver.WriteColorPixels     = write_color_pixels;
  385.    ctx->Driver.WriteMonocolorPixels = write_monocolor_pixels;
  386.    ctx->Driver.WriteIndexSpan       = write_index_span;
  387.    ctx->Driver.WriteMonoindexSpan   = write_monoindex_span;
  388.    ctx->Driver.WriteIndexPixels     = write_index_pixels;
  389.    ctx->Driver.WriteMonoindexPixels = write_monoindex_pixels;
  390.  
  391.    /* Pixel/span reading functions: */
  392.    /* TODO: use different funcs for 8, 16, 32-bit depths */
  393.    ctx->Driver.ReadIndexSpan = read_index_span;
  394.    ctx->Driver.ReadColorSpan = read_color_span;
  395.    ctx->Driver.ReadIndexPixels = read_index_pixels;
  396.    ctx->Driver.ReadColorPixels = read_color_pixels;
  397. }
  398.  
  399.  
  400.  
  401. /*
  402.  * Create a new VGA/Mesa context and return a handle to it.
  403.  */
  404. SVGAMesaContext SVGAMesaCreateContext( void )
  405. {
  406.    SVGAMesaContext ctx;
  407.    GLboolean rgb_flag;
  408.    GLfloat redscale, greenscale, bluescale, alphascale;
  409.    GLboolean db_flag = GL_FALSE;
  410.    GLboolean alpha_flag = GL_FALSE;
  411.    int colors;
  412.    GLint index_bits;
  413.  
  414.    /* determine if we're in RGB or color index mode */
  415.    colors = vga_getcolors();
  416.    if (colors==32768) {
  417.       rgb_flag = GL_TRUE;
  418.       redscale = greenscale = bluescale = alphascale = 255.0;
  419.       index_bits = 0;
  420.    }
  421.    else if (colors==256) {
  422.       rgb_flag = GL_FALSE;
  423.       redscale = greenscale = bluescale = alphascale = 0.0;
  424.       index_bits = 8;
  425.    }
  426.    else {
  427.       printf(">16 bit color not implemented yet!\n");
  428.       return NULL;
  429.    }
  430.  
  431.    ctx = (SVGAMesaContext) calloc( 1, sizeof(struct svgamesa_context) );
  432.    if (!ctx) {
  433.       return NULL;
  434.    }
  435.  
  436.    ctx->gl_vis = gl_create_visual( rgb_flag,
  437.                                    alpha_flag,
  438.                                    db_flag,
  439.                                    16,   /* depth_size */
  440.                                    8,    /* stencil_size */
  441.                                    16,   /* accum_size */
  442.                                    index_bits,
  443.                                    redscale,
  444.                                    greenscale,
  445.                                    bluescale,
  446.                                    alphascale );
  447.  
  448.    ctx->gl_ctx = gl_create_context( ctx->gl_vis,
  449.                                     NULL,  /* share list context */
  450.                                     (void *) ctx
  451.                                   );
  452.  
  453.    ctx->gl_buffer = gl_create_framebuffer( ctx->gl_vis );
  454.  
  455.    ctx->index = 1;
  456.    ctx->red = ctx->green = ctx->blue = 255;
  457.  
  458.    ctx->width = ctx->height = 0;  /* temporary until first "make-current" */
  459.  
  460.    return ctx;
  461. }
  462.  
  463.  
  464.  
  465.  
  466. /*
  467.  * Destroy the given VGA/Mesa context.
  468.  */
  469. void SVGAMesaDestroyContext( SVGAMesaContext ctx )
  470. {
  471.    if (ctx) {
  472.       gl_destroy_visual( ctx->gl_vis );
  473.       gl_destroy_context( ctx->gl_ctx );
  474.       gl_destroy_framebuffer( ctx->gl_buffer );
  475.       free( ctx );
  476.       if (ctx==SVGAMesa) {
  477.          SVGAMesa = NULL;
  478.       }
  479.    }
  480. }
  481.  
  482.  
  483.  
  484. /*
  485.  * Make the specified VGA/Mesa context the current one.
  486.  */
  487. void SVGAMesaMakeCurrent( SVGAMesaContext ctx )
  488. {
  489.    SVGAMesa = ctx;
  490.    gl_make_current( ctx->gl_ctx, ctx->gl_buffer );
  491.    svgamesa_setup_DD_pointers( ctx->gl_ctx );
  492.  
  493.    if (ctx->width==0 || ctx->height==0) {
  494.       /* setup initial viewport */
  495.       ctx->width = vga_getxdim();
  496.       ctx->height = vga_getydim();
  497.       gl_Viewport( ctx->gl_ctx, 0, 0, ctx->width, ctx->height );
  498.    }
  499. }
  500.  
  501.  
  502.  
  503. /*
  504.  * Return a handle to the current VGA/Mesa context.
  505.  */
  506. SVGAMesaContext SVGAMesaGetCurrentContext( void )
  507. {
  508.    return SVGAMesa;
  509. }
  510.  
  511.  
  512. /*
  513.  * Swap front/back buffers for current context if double buffered.
  514.  */
  515. void SVGAMesaSwapBuffers( void )
  516. {
  517.    if (SVGAMesa->gl_vis->DBflag) {
  518.       vga_flip();
  519.    }
  520. }
  521.  
  522.  
  523. #else
  524.  
  525. /*
  526.  * Need this to provide at least one external definition when SVGA is
  527.  * not defined on the compiler command line.
  528.  */
  529.  
  530. int gl_svga_dummy_function(void)
  531. {
  532.    return 0;
  533. }
  534.  
  535. #endif  /*SVGA*/
  536.  
  537.